package com.liu.student.controller;

import com.liu.student.dto.ApiResponse;
import com.liu.student.entity.Student;
import com.liu.student.entity.Teacher;
import com.liu.student.entity.Course;
import com.liu.student.service.StudentService;
import com.liu.student.service.TeacherService;
import com.liu.student.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员控制器
 */
@Controller
@RequestMapping("/admin")
@PreAuthorize("hasRole('ADMIN')")
public class AdminController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;

    @GetMapping("/dashboard")
    public String dashboard(Model model) {
        // 获取统计数据
        long studentCount = studentService.count();
        long teacherCount = teacherService.count();
        long courseCount = courseService.count();
        
        model.addAttribute("studentCount", studentCount);
        model.addAttribute("teacherCount", teacherCount);
        model.addAttribute("courseCount", courseCount);
        
        return "admin/dashboard";
    }

    // 学生管理
    @GetMapping("/students")
    public String students(Model model) {
        List<Student> students = studentService.findAll();
        model.addAttribute("students", students);
        return "admin/students";
    }

    @GetMapping("/students/add")
    public String addStudentForm(Model model) {
        model.addAttribute("student", new Student());
        return "admin/student-form";
    }

    @PostMapping("/students")
    public String addStudent(@ModelAttribute Student student) {
        studentService.save(student);
        return "redirect:/admin/students";
    }

    @GetMapping("/students/edit/{id}")
    public String editStudentForm(@PathVariable Long id, Model model) {
        Student student = studentService.findById(id);
        model.addAttribute("student", student);
        return "admin/student-form";
    }

    @PostMapping("/students/update")
    public String updateStudent(@ModelAttribute Student student) {
        studentService.update(student);
        return "redirect:/admin/students";
    }

    @GetMapping("/students/delete/{id}")
    public String deleteStudent(@PathVariable Long id) {
        studentService.deleteById(id);
        return "redirect:/admin/students";
    }

    // 教师管理
    @GetMapping("/teachers")
    public String teachers(Model model) {
        List<Teacher> teachers = teacherService.findAll();
        model.addAttribute("teachers", teachers);
        return "admin/teachers";
    }

    @GetMapping("/teachers/add")
    public String addTeacherForm(Model model) {
        model.addAttribute("teacher", new Teacher());
        return "admin/teacher-form";
    }

    @PostMapping("/teachers")
    public String addTeacher(@ModelAttribute Teacher teacher) {
        teacherService.save(teacher);
        return "redirect:/admin/teachers";
    }

    @GetMapping("/teachers/edit/{id}")
    public String editTeacherForm(@PathVariable Long id, Model model) {
        Teacher teacher = teacherService.findById(id);
        model.addAttribute("teacher", teacher);
        return "admin/teacher-form";
    }

    @PostMapping("/teachers/update")
    public String updateTeacher(@ModelAttribute Teacher teacher) {
        teacherService.update(teacher);
        return "redirect:/admin/teachers";
    }

    @GetMapping("/teachers/delete/{id}")
    public String deleteTeacher(@PathVariable Long id) {
        teacherService.deleteById(id);
        return "redirect:/admin/teachers";
    }

    // 课程管理
    @GetMapping("/courses")
    public String courses(Model model) {
        List<Course> courses = courseService.findAll();
        model.addAttribute("courses", courses);
        return "admin/courses";
    }

    @GetMapping("/courses/add")
    public String addCourseForm(Model model) {
        model.addAttribute("course", new Course());
        List<Teacher> teachers = teacherService.findAll();
        model.addAttribute("teachers", teachers);
        return "admin/course-form";
    }

    @PostMapping("/courses")
    public String addCourse(@ModelAttribute Course course) {
        courseService.save(course);
        return "redirect:/admin/courses";
    }

    @GetMapping("/courses/edit/{id}")
    public String editCourseForm(@PathVariable Long id, Model model) {
        Course course = courseService.findById(id);
        model.addAttribute("course", course);
        List<Teacher> teachers = teacherService.findAll();
        model.addAttribute("teachers", teachers);
        return "admin/course-form";
    }

    @PostMapping("/courses/update")
    public String updateCourse(@ModelAttribute Course course) {
        courseService.update(course);
        return "redirect:/admin/courses";
    }

    @GetMapping("/courses/delete/{id}")
    public String deleteCourse(@PathVariable Long id) {
        courseService.deleteById(id);
        return "redirect:/admin/courses";
    }
}
